package com.control.qct.businesssteward.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.IInterface;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.activity.PaymentActivity;
import com.control.qct.businesssteward.adapter.PaymentPromotionAdapter;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.DoPayGoodsModel;
import com.control.qct.businesssteward.model.DoPayPromotionGoodsModel;
import com.control.qct.businesssteward.model.OpenOrderModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.GsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 结算界面1--促销活动
 * Created by Administrator on 2017/12/3 0003.
 */

public class PaymentFragment extends BaseFragment implements PaymentPromotionAdapter.IPromotionCallbackListener {

    @BindView(R.id.tv_receivable)
    public TextView mTvReceivable;//应付金额
    @BindView(R.id.tv_goods_num)
    public TextView mTvGoodsNum;//购买的商品数量
    @BindView(R.id.tv_goods_discounts)
    public TextView mTvGoodsDiscounts;//优惠价
    @BindView(R.id.tv_payment)
    TextView mTvPayment;//结算
    @BindView(R.id.rv_payment_promotion)
    RecyclerView mRvPaymentPromotion;
    private PaymentActivity mPaymentActivity;
    private OpenOrderModel.CarItemsBean.TotalBean mTotalBean;
    private List<DoPayPromotionGoodsModel> mDoPayPromotionGoodsModelList;
    private List<DoPayPromotionGoodsModel> mSelectDoPayPromotionGoodsModelList = new ArrayList<>();
    private String mPromotionJson;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPaymentActivity = (PaymentActivity) context;
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_payment, container, false);
    }

    @Override
    protected void initView(View view) {
        mTotalBean = mPaymentActivity.mTotalBean;
        mTvReceivable.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(mTotalBean.getAmount()));
        mTvGoodsNum.setText("共" + mTotalBean.getCount() + "件商品");
        mTvGoodsDiscounts.setText("已优惠 ￥" + DecimalFormatUtils.DecimalFormatTwo(mTotalBean.getPreferential()));

        mDoPayPromotionGoodsModelList = mPaymentActivity.mDoPayPromotionGoodsModelList;
        mRvPaymentPromotion.setLayoutManager(new LinearLayoutManager(mPaymentActivity));
        mRvPaymentPromotion.setAdapter(new PaymentPromotionAdapter(mDoPayPromotionGoodsModelList,
                this));
    }

    @OnClick({R.id.tv_payment})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_payment://结算
                mPaymentActivity.gotoNewPayGoods(mPromotionJson);
                break;
        }
    }


    Map<Integer, DoPayPromotionGoodsModel> data = new HashMap<>();

    @Override
    public void callbackPromotionResult(int position, List<DoPayPromotionGoodsModel.ItemsBean> item) {
        List<DoPayPromotionGoodsModel.ItemsBean> itemsBeanList = new ArrayList<>();
        for (int i = 0; i < item.size(); i++) {
            if (item.get(i).isChecked()) {
                itemsBeanList.add(item.get(i));
            }
        }
        DoPayPromotionGoodsModel doPayPromotionGoodsModel = new DoPayPromotionGoodsModel();
        doPayPromotionGoodsModel.setType(mDoPayPromotionGoodsModelList.get(position).getType());
        doPayPromotionGoodsModel.setDescription(mDoPayPromotionGoodsModelList.get(position).getDescription());
        doPayPromotionGoodsModel.setSelectWay(mDoPayPromotionGoodsModelList.get(position).getSelectWay());
        doPayPromotionGoodsModel.setMarketingSN(mDoPayPromotionGoodsModelList.get(position).getMarketingSN());
        doPayPromotionGoodsModel.setMarketingDescription(mDoPayPromotionGoodsModelList.get(position)
                .getMarketingDescription());
        doPayPromotionGoodsModel.setItems(itemsBeanList);
        data.put(position, doPayPromotionGoodsModel);
        if (itemsBeanList.size() == 0) {
            data.remove(position);
        }
        mSelectDoPayPromotionGoodsModelList.clear();
        for (Map.Entry<Integer, DoPayPromotionGoodsModel> doPayPromotionGoodsModel1 : data.entrySet()) {
            mSelectDoPayPromotionGoodsModelList.add(doPayPromotionGoodsModel1.getValue());
        }
        mPromotionJson = GsonUtil.GsonString(mSelectDoPayPromotionGoodsModelList);
    }


}
